package com.hyfd.task;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import com.hyfd.common.utils.GetUtil;
import com.hyfd.common.utils.MD5;
import com.hyfd.common.utils.ToolHttp;
import com.hyfd.common.utils.XmlUtils;
import com.hyfd.dao.mp.OrderDao;
import com.hyfd.dao.mp.ProviderPhysicalChannelDao;
import com.hyfd.rabbitMq.RabbitMqProducer;
import com.hyfd.rabbitMq.SerializeUtil;

@Component
public class ZhuoYiFlowTask {

	@Autowired
	ProviderPhysicalChannelDao providerPhysicalChannelDao;//物理通道信息
	@Autowired
	OrderDao orderDao;//订单
	@Autowired
	RabbitMqProducer mqProducer;//消息队列生产者
	private static Logger log = Logger.getLogger(BoLuoMiNewFlowTask.class);
	
//	@Scheduled(fixedDelay = 60000)
	public void queryBoLuoMiNewOrder(){
		Map<String,Object> map = new HashMap<String,Object>();

		int flag = -1;
		try{
			String id = "0000000102";
			String action = "Query";
			Map<String,Object> channel = providerPhysicalChannelDao.selectByPrimaryKey(id);//获取通道的数据
			String defaultParameter = (String) channel.get("default_parameter");//默认参数
			Map<String,String> paramMap = XmlUtils.readXmlToMap(defaultParameter);
			String queryOrderUrl = paramMap.get("queryOrderUrl");
			String account  = paramMap.get("account");// 账户名
            String apikey = paramMap.get("apikey");
			Map<String,Object> param = new HashMap<String,Object>();
			param.put("dispatcherProviderId", id);
			param.put("status", "1");
			List<Map<String,Object>> orderList = orderDao.selectByTask(param);
			for(Map<String,Object> order : orderList){
				String orderId = order.get("orderId")+"";
				map.put("orderId", orderId);
				String order_num = order.get("providerOrderId")+"";
				long timeStamp = new Date().getTime()/1000;
				String params = apikey+"account="+account+"&action="+action+"&orderID="+order_num+"&timeStamp="+timeStamp+apikey;
	            String sign = MD5.ToMD5(params);
//	            JsonObject data = new JsonObject();
//	    		data.addProperty("token", token);
//	    		data.addProperty("order_num", order_num);
//	    		data.addProperty("sign", sign);// 设置签名
	    		String par =  "account="+account+"&action="+action+"&orderID="+order_num+"&timeStamp="+timeStamp+ "&sign=" + sign;
	    		String contentType = "application/x-www-form-urlencoded";
	    		String postUrl = queryOrderUrl;
	    		String retMsg = ToolHttp.post(false, postUrl, par, contentType);
				if(null!=retMsg&&!"".equals(retMsg)){
					JSONObject jObject = JSONObject.parseObject(retMsg);
	                String resultCode = jObject.getString("respCode");
	                String resultMsg = jObject.getString("respMsg");
	               
                	map.put("resultCode", resultCode + ":" + resultMsg);
                	if("0001".equals(resultCode)){
                		continue;
                	}else if("0002".equals(resultCode)){
                		flag=1;
                	}else{
                		flag=0;
                	}
				
				map.put("status", flag);
				mqProducer.sendDataToQueue(RabbitMqProducer.Result_QueueKey, SerializeUtil.getStrFromObj(map));
				}
			}
		}catch(Exception e){
			log.error("流量查询Task出错"+e);
		}
	}
	
	
}
